package proxy
{
    import com.pamakids.models.ResultVO;
    import com.pamakids.services.ServiceBase;
    import com.pamakids.utils.CloneUtil;
    
    import mx.controls.Alert;
    
    import controllers.API;
    
    import models.UserVO;
    import models.VO;
    
    import service.SOService;
    
    /**
     *
     * @author Administrator
     */
    public class LoginProxy
    {
        private var errorHandler:Function, compHandler:Function;
        
        public function LoginProxy()
        {
        }
        
        /**
         * 解析用户名，如果是有效手机号则返回0， 如果是有效邮件则返回1，若用户名无效，则返回-1
         */
        public static function parseUserName(username:String):int
        {
            var pattern_mobile:RegExp = /^[1][358]\d{9}$/;
            var pattern_email:RegExp = /([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}/;
            if(username.search(pattern_mobile) != -1)
                return 0;
            else if(username.search(pattern_email) != -1)
                return 1;
            return -1;
        }
        
        /**
         *
         * @param type mail,platform
         * @param data {username,password}...
         * @param _errorHandler
         * @param _compHandlerW
         */
//		public function login(type:String, data:Object, _errorHandler:Function, _compHandler:Function):void
//		{
//			errorHandler=_errorHandler;
//			compHandler=_compHandler;
//			if (type == "mail")
//				API.instance.login(data.username, data.password, mailCallback);
//			else
//			{
//				API.instance.loginFromPlatform(data, platformCallback);
//			}
//		}
        
        private function platformCallback(o:ResultVO):void
        {
            if (o.status == true)
            {
                compHandler(o.results);
            }
            else
            {
                errorHandler(o.results)
            }
        }
        
        private function mailCallback(o:ResultVO):void
        {
            if (o.status == true)
            {
                compHandler(o.results);
            }
            else
            {
                errorHandler(o.results)
            }
        }
        
//		public static function autoLogin():void
//		{
//			var username:String='test1';
//			var pw:String='123456';
//			API.instance.login(username, pw, loginCallback);
//		}
        
        private static function parsePlatformData(s:String):void
        {
            var obj:Object=JSON.parse(s);
            var token:String=obj.accessToken;
            var portrait:String=obj.iconURL;
            var platform:String=obj.platformName;
            var username:String=obj.userName;
            var usid:int=obj.usid;
            
            API.instance.loginFromPlatform({
                    "come_from": VO.APPNAME,
                    "access_token": token,
                    "platform": platform,
                    "usid": usid,
                    "portrait": portrait,
                    "nickname": username
                }, loginCallback);
        }
        
        private static function loginCallback(o:ResultVO):void
        {
            if (o.status == true)
            {
                saveLoginInfo(CloneUtil.convertObject(o.results, UserVO));
            }
            else
            {
            }
        }
        
        private static function saveLoginInfo(uv:UserVO):void
        {
            UserVO.crtUser=uv;
            ServiceBase.id=uv._id;
            API.instance.initToken();
            
            var user:UserVO=UserVO.crtUser;
        }
        
        public static function clearLoginInfo():void
        {
            UserVO.crtUser=null;
            ServiceBase.id="";
            FileProxy.token="";
            FileProxy.audioToken="";
            SOService.setValue("lastPlatForm", "");
        }
        
        private static var loggedCB:Function
        
        public static function autoQQLogin(cb:Function,arr:Array):void
        {
            loggedCB=cb;
            //usid:String, access_token:String, nickname:String, portrait:String
            API.instance.platformLogin(webCB,arr[0],arr[1],arr[2],arr[3]);
        }
        
        private static function webCB(o:ResultVO):void
        {
            if (o.status == true)
            {
                saveLoginInfo(CloneUtil.convertObject(o.results, UserVO));
                if(loggedCB!=null)
                    loggedCB();
                loggedCB=null;
            }
            else
            {
                Alert.show('系统升级,敬请期待','提示',4,null);
            }
        }
        
        public static function webLogin(username:String,pw:String,cb:Function):void
        {
            loggedCB=cb;
            API.instance.webLogin(username,pw,'','','','',webCB,'','','','');
        }
    }
}


